//
//  MXNewViewItemDetailTableViewCell.m
//  MXの衣柜
//
//  Created by 马霄 on 15/8/16.
//  Copyright (c) 2015年 霄 马. All rights reserved.
//

#import "MXNewViewItemDetailTableViewCell.h"
#import "MXFindWebViewVC.h"

@interface MXNewViewItemDetailTableViewCell ()

@property (nonatomic, retain) UILabel *locationLabel;

// 安放价格和查看详情的Label
@property (nonatomic, retain) UILabel *InfoLabel;

// 查看详情
@property (nonatomic, retain) UILabel *checkLabel;

@property (nonatomic, retain) UIView *topLine;

@property (nonatomic, retain) UIView *downLine;

@property (nonatomic, retain) UIView *carveUpLine;

@property (nonatomic, retain) NSString *TmallLinkString;
@end

@implementation MXNewViewItemDetailTableViewCell
- (void)dealloc
{
    [_TmallLinkString release];
    [_naviDetail release];
    [_carveUpLine release];
    [_topLine release];
    [_downLine release];
    [_InfoLabel release];
    [_checkLabel release];
    [_tempString release];
    [_model release];
    [_locationLabel release];
    [_commentLabel release];
    [_titleLabel release];
    [_priceLabel release];
    [_shopLabel release];
    [super dealloc];
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.titleLabel = [[[UILabel alloc ]init] autorelease];
        self.titleLabel.numberOfLines = 0;
        self.titleLabel.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:self.titleLabel];
        
        self.shopLabel = [[[UILabel alloc] init] autorelease];
        self.shopLabel.backgroundColor = [UIColor whiteColor];
        self.shopLabel.font = [UIFont boldSystemFontOfSize:17];
        [self.contentView addSubview:self.shopLabel];
        
        self.commentLabel = [[[UILabel alloc] init] autorelease];
        self.commentLabel.backgroundColor = [UIColor whiteColor];
        self.commentLabel.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:self.commentLabel];
        
        self.locationLabel = [[[UILabel alloc] init] autorelease];
        self.locationLabel.backgroundColor = [UIColor whiteColor];
        self.locationLabel.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:self.locationLabel];
        
        self.InfoLabel = [[[UILabel alloc] init] autorelease];
        self.InfoLabel.backgroundColor = [UIColor blackColor];
        self.InfoLabel.userInteractionEnabled = YES;
        self.InfoLabel.layer.cornerRadius = 13;
        self.InfoLabel.layer.masksToBounds = YES;
        self.InfoLabel.text = @"";
        [self.contentView addSubview:self.InfoLabel];
        
        self.priceLabel = [[[UILabel alloc] init] autorelease];
        self.priceLabel.backgroundColor = [UIColor blackColor];
        self.priceLabel.font = [UIFont systemFontOfSize:16];
        self.priceLabel.textAlignment = NSTextAlignmentCenter;
        [self.priceLabel setTextColor:[UIColor whiteColor]];
        [self.InfoLabel addSubview:self.priceLabel];

        self.checkLabel = [[[UILabel alloc] init] autorelease];
        self.checkLabel.backgroundColor = [UIColor blackColor];
        [self.checkLabel setTextColor:[UIColor whiteColor]];
        self.checkLabel.textAlignment = NSTextAlignmentCenter;
        self.checkLabel.userInteractionEnabled = YES;
        self.checkLabel.text = @"查看详情";
        self.checkLabel.font = [UIFont systemFontOfSize:16];
        [self.InfoLabel addSubview:self.checkLabel];
        // 添加点击手势
        UITapGestureRecognizer *tap  = [[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(checkDetailAction)] autorelease];
        [self.checkLabel addGestureRecognizer:tap];
        
        
        
        self.topLine = [[[UIView alloc] init] autorelease];
        self.topLine.backgroundColor = [UIColor grayColor];
        [self.contentView addSubview:self.topLine];
        
        self.downLine = [[[UIView alloc] init] autorelease];
        self.downLine.backgroundColor = [UIColor grayColor];
        [self.contentView addSubview:self.downLine];
        
        self.carveUpLine = [[[UIView alloc] init] autorelease];
        self.carveUpLine.backgroundColor = [UIColor whiteColor];
        [self.InfoLabel addSubview:self.carveUpLine];
        
    }
    return self;
}
-(void)layoutSubviews
{
    CGFloat WID = self.frame.size.width;
    
    CGFloat titleLabelX = WIDTHPRO * 10;
    CGFloat titleLabelY = HEIGHTPRO * 20;
    CGFloat titleLabelW = WID - 2 * titleLabelX;
    CGFloat titleLabelH = HEIGHTPRO * 60;
    self.titleLabel.frame = CGRectMake(titleLabelX, titleLabelY, titleLabelW, titleLabelH);
    
    CGFloat InfoLabelX = WID / 2 - WIDTHPRO * 10;
    CGFloat InfoLabelY = titleLabelH + titleLabelY + HEIGHTPRO * 20;
    CGFloat InfoLabelW = WID / 2 - WIDTHPRO * 20;
    CGFloat InfoLabelH = HEIGHTPRO * 40;
    self.InfoLabel.frame = CGRectMake(InfoLabelX, InfoLabelY, InfoLabelW, InfoLabelH);
    
    CGFloat priceLabelX = WIDTHPRO * 10;
    CGFloat priceLabelY = HEIGHTPRO * 5;
    CGFloat priceLabelW = InfoLabelW / 3 -  WIDTHPRO * 5;
    CGFloat priceLabelH = HEIGHTPRO * 30;
    self.priceLabel.frame = CGRectMake(priceLabelX, priceLabelY, priceLabelW, priceLabelH);
    
    CGFloat carveUpLineX = priceLabelW + priceLabelX + WIDTHPRO * 5;
    CGFloat carveUpLineY = priceLabelY;
    CGFloat carveUpLineW = HEIGHTPRO * 1;
    CGFloat carveUpLineH = InfoLabelH - 2 * priceLabelY;
    self.carveUpLine.frame = CGRectMake(carveUpLineX, carveUpLineY, carveUpLineW, carveUpLineH);
    
    CGFloat checkLabelX = priceLabelW + priceLabelX + WIDTHPRO * 10;
    CGFloat checkLabelY = priceLabelY;
    CGFloat checkLabelW = InfoLabelW - checkLabelX - WIDTHPRO * 10;
    CGFloat checkLabelH = priceLabelH;
    self.checkLabel.frame = CGRectMake(checkLabelX, checkLabelY, checkLabelW, checkLabelH);

    
    CGFloat shopLabelX = titleLabelX;
    CGFloat shopLabelY = InfoLabelH + InfoLabelY + HEIGHTPRO *  25;
    CGFloat shopLabelW = WIDTHPRO * 200;
    CGFloat shopLabelH = HEIGHTPRO * 30;
    self.shopLabel.frame = CGRectMake(shopLabelX, shopLabelY, shopLabelW, shopLabelH);
    
    CGFloat locationLabelX = shopLabelX;
    CGFloat locationLabelY = shopLabelH + shopLabelY + HEIGHTPRO * 10;
    CGFloat locationLabelW = WIDTHPRO * 100;
    CGFloat locationLabelH = HEIGHTPRO * 20;
    self.locationLabel.frame = CGRectMake(locationLabelX, locationLabelY, locationLabelW, locationLabelH);
    
    CGFloat commentLabelX = locationLabelX + locationLabelW + WIDTHPRO * 10;
    CGFloat commentLabelY = shopLabelH + shopLabelY + HEIGHTPRO * 10;
    CGFloat commentLabelW = WIDTHPRO * 150;
    CGFloat commentLabelH = HEIGHTPRO * 20;
    self.commentLabel.frame = CGRectMake(commentLabelX, commentLabelY, commentLabelW, commentLabelH);
    
    self.topLine.frame = CGRectMake(shopLabelX, shopLabelY - WIDTHPRO * 5, WID - 2 * shopLabelX,HEIGHTPRO * 1);
    
    self.downLine.frame = CGRectMake(shopLabelX, locationLabelH + locationLabelY + HEIGHTPRO * 5, WID - 2 * shopLabelX, HEIGHTPRO * 1);
    
    
}


- (void)getInfo:(MXNewViewItemDetailModel *)model Price:(NSString *)price
{
    if (_model != model) {
        [_model release];
        _model = [model retain];
    }
    self.titleLabel.text = model.title;
    
    self.locationLabel.text = model.location;
    
    self.commentLabel.text = [NSString stringWithFormat:@"好评率 %@", [model.seller objectForKey:@"goodRatePercentage"]];
    
    self.shopLabel.text = [model.seller objectForKey:@"shopTitle"];
    
    self.TmallLinkString = model.itemUrl;
    
    CGFloat tempPrice = [price floatValue];
    
    self.priceLabel.text = [NSString stringWithFormat:@"¥%.0f", tempPrice];
}


- (void)checkDetailAction
{
        
    MXFindWebViewVC *webViewVC = [[[MXFindWebViewVC alloc] init] autorelease];
    
    webViewVC.requestString = self.TmallLinkString;
    
    webViewVC.TitleString = @"商品详情";
    
    [self.naviDetail pushViewController:webViewVC animated:YES];

}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
